<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use GatewayWorker\Lib\Gateway;
use think\Exception;

class Send extends BaseController
{
    public function user()
    {
        try{
            $params=$this->request->param();

            $msgContent=$params['content'];

            $to_uid=$params['app_id'].'_'.$params['uid'];

            $isOnline=Gateway::isUidOnline($to_uid);

            if ($isOnline){
                Gateway::sendToUid($to_uid,json_encode($msgContent));
                $this->apiJson(0,'','发送成功');
            }else{
                $this->apiJson(1,'','此付款用户暂时离线,无法使用');
            }

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }

    public function group(){
        try{
            $params=$this->request->param();

            $msgContent=$params['content'];

            $to_group_id=$params['app_id'].'_'.$params['group_id'];

            Gateway::sendToGroup($to_group_id,json_encode($msgContent));
            $this->apiJson(0,'','发送成功');


        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }

    public function all(){
        try{
            $params=$this->request->param();

            $msgContent=$params['content'];

            $to_group_id=$params['app_id'];

            Gateway::sendToGroup($to_group_id,json_encode($msgContent));
            $this->apiJson(0,'','发送成功');

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }
}
